package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDTO;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.service.NursingProjectPlanService;
import com.zzyl.vo.NursingPlanVO;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Transactional
@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanService nursingProjectPlanService;

    //修改护理计划
    @Override
    @Transactional
    public void updateNursingPlan(NursingPlanDTO nursingPlan) {
        NursingPlan plan = BeanUtil.toBean(nursingPlan, NursingPlan.class);
        //拿到根据id查询的方法类型，获取到id
        NursingPlanVO oldPlan = selectById(plan.getId());
        //集合转换stm流拿到id
        List<Long> ids = oldPlan.getProjectPlans().stream()
                .map(NursingProjectPlanVo::getId).collect(Collectors.toList());
        //删除
        nursingProjectPlanService.removeList(ids);
        //判断集合是否为空，为空则删除，不为空则修改，循环
        if (CollUtil.isNotEmpty(nursingPlan.getProjectPlans())) {
            for (NursingProjectPlanDto v : nursingPlan.getProjectPlans()) {
                v.setPlanId(plan.getId());
                //添加
                nursingProjectPlanService.addList(nursingPlan.getProjectPlans());
            }
            //更新
            nursingPlanMapper.Update(plan);
        }

    }

    //添加护理计划
    @Override
    @Transactional
    public void addNursingPlan(NursingPlanDTO nursingPlan) {
        //转换
        NursingPlan plan = BeanUtil.toBean(nursingPlan, NursingPlan.class);
        plan.setStatus(1);
        //添加护理计划
        nursingPlanMapper.addNursingPlan(plan);
        //循环添加
        nursingPlan.getProjectPlans().forEach(v -> v.setPlanId(plan.getId()));
        //添加护理项目计划
        nursingProjectPlanService.addList(nursingPlan.getProjectPlans());
    }

    //条件分页查询
    @Override
    public PageResponse<NursingPlanVO> selectByPage(String name, Integer pageNum, Integer pageSize, Integer status) {
        //开启分页插件拿到num和size
        PageHelper.startPage(pageNum, pageSize);
        //拿到name和status
        Page<NursingPlan> nursingPlans = nursingPlanMapper.selectByPage(name, status);
        //转换vo
        PageResponse<NursingPlanVO> nursingPlanVoPageResponse = PageResponse.of(nursingPlans, NursingPlanVO.class);
        return nursingPlanVoPageResponse;
    }

    //查询所有护理计划
    @Override
    public List<NursingPlanVO> selectAll() {
        //集合封装
        List<NursingPlan> nursingPlans = nursingPlanMapper.selectAll();
        //vo转换
        return BeanUtil.copyToList(nursingPlans, NursingPlanVO.class);
    }

    //根据id查询护理计划
    @Override
    public NursingPlanVO selectById(Long id) {
        return nursingPlanMapper.selectById(id);
    }

    //删除护理计划
    @Override
    public void removeNursingPlan(Long id) {
        //删除关联表
        nursingProjectPlanService.deleteByNursingPlanId(id);

        nursingPlanMapper.deleteById(id);
    }

    //启用/禁用
    @Override
    public void enableOrDisable(Long id, Integer status) {
        nursingPlanMapper.updateStatus(id, status);
    }
}
